﻿using System.ComponentModel;
using System.IO.IsolatedStorage;
using System.Windows;
using System.Windows.Controls;
using AdminApp.Web.Services;
using TheOliver.DigitalSignage.Client.Interfaces;
using TheOliver.DigitalSignage.Client.Logic;

namespace TheOliver.DigitalSignage.Client.Windows
{
    public partial class SelectRoomWindow : ChildWindow
    {
        public SelectRoomWindow()
        {
            InitializeComponent();

            if (DesignerProperties.IsInDesignTool)
            {
                return;
            }

            _eventId.Text = Configuration.SelectedEvent.Id.ToString();
            roomDomainDataSource.Load();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            Configuration.SelectedRoom = comboBox1.SelectedItem as AdminApp.Web.Room;
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void roomDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {
            if (e.HasError)
            {
                Helper.ShowError(e.Error); 
                e.MarkErrorAsHandled();
            }
            else
            {
                if (Configuration.SelectedRoom != null)
                {
                    DigitalSignageDomainContext context = 
                        roomDomainDataSource.DomainContext as DigitalSignageDomainContext;
                    foreach (var r in context.Rooms)
                    {
                        if (r.Id == Configuration.SelectedRoom.Id)
                        {
                            comboBox1.SelectedItem = r;
                            break;
                        }
                    }
                }
                OKButton.IsEnabled = true;
                OKButton.Focus();
            }
        }

        private void roomDomainDataSourceLoadButton_Click(object sender, RoutedEventArgs e)
        {
            roomDomainDataSource.Load();
        }
        
    }
}

